@file:OptIn(ExperimentalMaterial3Api::class)

package com.kira.solo.schedule.list

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.kira.solo.schedule.R
import com.kira.solo.schedule.pojo.CasePeriodTaskInfo

/**
 * 定时任务列表
 * @packageName com.kira.solo.schedule.list
 * @date 2024/1/24 13:50
 * @author zhanglei
 */
@Composable
fun ScheduleList(
    viewModel: ScheduleListViewModel = hiltViewModel(),
    editTask: (Long) -> Unit,
    onFinish: () -> Unit,
) {
    val cases by viewModel.cases.collectAsState(initial = ScheduledCaseListUiState.INIT)
    Scaffold(topBar = {
        TopAppBar(onFinish) { editTask(-1L) }
    }) {
        Box(
            modifier = Modifier
                .padding(it)
                .fillMaxSize()
        ) {
            CaseList(
                cases,
                onEdit = { info -> editTask(info.taskId) },
                onDelete = viewModel::onDeleteTask
            )
        }
    }
}

@Composable
fun TopAppBar(onBack: () -> Unit, editTask: () -> Unit) {
    CenterAlignedTopAppBar(
        colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
            containerColor = colorResource(
                id = R.color.colorPrimary
            ),
            navigationIconContentColor = Color.Transparent,
            actionIconContentColor = Color.Transparent,
        ),
        title = {
            Text(
                text = "定时任务",
                color = Color.White
            )
        },
        navigationIcon = {
            Button(
                onClick = onBack,
                colors = ButtonDefaults.buttonColors(containerColor = Color.Transparent)
            ) {
                Image(
                    painter = painterResource(id = R.drawable.head_icon_back),
                    contentDescription = "返回"
                )
            }
        },
        actions = {
            Button(
                onClick = editTask,
                colors = ButtonDefaults.buttonColors(containerColor = Color.Transparent)
            ) {
                Text(text = "新增任务", fontSize = 18.sp)
            }
        })
}

@Composable
fun CaseList(
    uiState: ScheduledCaseListUiState,
    onDelete: (CasePeriodTaskInfo) -> Unit,
    onEdit: (CasePeriodTaskInfo) -> Unit,
) {
    LazyColumn {
        items(uiState.cases,
            key = { it.casePeriodTaskInfo.taskId }) { case ->
            TaskInfoLayout(
                case.casePeriodTaskInfo,
                onEdit = onEdit,
                onDelete = onDelete,
            ) {
                Column(Modifier.padding(15.dp)) {
                    case.cases.forEach {
                        CaseWithDetailLayout(it)
                        Spacer(modifier = Modifier.height(5.dp))
                    }
                }
            }
        }
    }
}

@Composable
fun TaskInfoLayout(
    uiState: CasePeriodTaskInfo,
    onDelete: (CasePeriodTaskInfo) -> Unit,
    onEdit: (CasePeriodTaskInfo) -> Unit,
    items: @Composable () -> Unit,
) {
    Column(
        modifier = Modifier
            .padding(5.dp)
            .fillMaxWidth()
            .border(1.dp, color = Color.Blue, RoundedCornerShape(3.dp))
    ) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .height(70.dp),
        ) {
            Box(modifier = Modifier.fillMaxSize()) {

                Column(
                    modifier = Modifier
                        .align(Alignment.Center)
                        .padding(10.dp)
                ) {
                    CaseDetailItem("触发时间", uiState.time.display)
                    CaseDetailItem("触发周期", uiState.periodType.display)
                }

                Button(
                    onClick = { onDelete(uiState) },
                    modifier = Modifier
                        .align(Alignment.CenterStart)
                        .height(30.dp)
                        .width(50.dp),
                    colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray),
                    contentPadding = PaddingValues(3.dp)
                ) {
                    Image(
                        modifier = Modifier.fillMaxSize(),
                        painter = painterResource(id = R.drawable.dialog_action_drawable_power),
                        contentDescription = "删除",
                    )
                }

                Button(
                    onClick = { onEdit(uiState) },
                    modifier = Modifier
                        .align(Alignment.CenterEnd)
                        .height(30.dp)
                        .width(50.dp),
                    colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray),
                    contentPadding = PaddingValues(3.dp)
                ) {
                    Image(
                        modifier = Modifier.fillMaxSize(),
                        painter = painterResource(id = R.drawable.dialog_action_drawable_device_operation),
                        contentDescription = "编辑",
                    )
                }
            }
        }
        Spacer(modifier = Modifier.height(5.dp))
        items()
    }

}

@Composable
fun CaseLayout(uiState: CaseUiState, onCaseClick: () -> Unit) {
    Card(
        onClick = onCaseClick,
        modifier = Modifier
            .fillMaxWidth()
            .height(100.dp)
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            Column(
                modifier = Modifier
                    .align(Alignment.CenterStart)
                    .padding(start = 10.dp)
            ) {
                Text(text = uiState.caseName, color = Color.Black, fontSize = 18.sp)
                Spacer(modifier = Modifier.height(5.dp))
                Text(text = uiState.caseDesc, color = Color.Gray, fontSize = 16.sp)
            }

            Column(
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 10.dp)
            ) {
                Text(
                    text = uiState.targetAppLabel,
                    color = Color.Black,
                    fontSize = 12.sp,
                    modifier = Modifier.align(Alignment.End)
                )
                Text(
                    text = uiState.targetAppPackage,
                    color = Color.Black,
                    fontSize = 12.sp,
                    modifier = Modifier.align(Alignment.End)
                )
                Spacer(modifier = Modifier.height(5.dp))
                Text(text = uiState.gmtModify, color = Color.Gray, fontSize = 12.sp)
            }
        }
    }
}

@Composable
fun CaseWithDetailLayout(uiState: CaseUiState) {
    var detailState by remember { mutableStateOf(false) }
    Column {
        Box {
            CaseLayout(uiState = uiState) {}

            Button(
                onClick = { detailState = detailState.not() },
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .height(30.dp)
                    .width(50.dp),
                colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray),
                contentPadding = PaddingValues(3.dp)
            ) {
                Image(
                    modifier = Modifier.fillMaxSize(),
                    painter = painterResource(id = if (detailState) R.drawable.dialog_action_drawable_pinch_in else R.drawable.dialog_action_drawable_pinch_out),
                    contentDescription = "展开",
                )
            }
        }

        AnimatedVisibility(visible = detailState) {
            Column(
                modifier = Modifier
                    .padding(horizontal = 15.dp)
                    .background(Color.Yellow)
                    .alpha(0.5f)
                    .fillMaxWidth()
            ) {
                CaseDetailItem("包名信息", uiState.targetAppPackage)
                CaseDetailItem("应用名称", uiState.targetAppLabel)
                CaseDetailItem("记录方式", uiState.recordMode)
                CaseDetailItem("高级设置", uiState.advanceSettings)
                CaseDetailItem("日志信息", uiState.operationLog)
                CaseDetailItem("生成时间", uiState.gmtCreate)
                CaseDetailItem("修改时间", uiState.gmtModify)
            }
        }
    }
}

@Composable
fun CaseDetailItem(title: String, content: String) {
    Column {
        Row {
            Text(text = "$title : ", color = Color.Red, fontSize = 13.sp)
            Text(text = content, color = Color.Black, fontSize = 12.sp)
        }
        Spacer(modifier = Modifier.height(5.dp))
    }
}